package com.wwl.controller;

import com.wwl.entity.Location;
import com.wwl.entity.Unitdoor;
import com.wwl.service.LocationService;
import com.wwl.service.UnitdoorService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/l")
public class LocationController{
	
  Logger logger = Logger.getLogger(LocationController.class);
  @Resource
  LocationService locationService;
  @Resource
  UnitdoorService unitdoorService;
  
  String state_500="500";			//表示服务器内部错误
  String state_0="0";				//表示请求服务器成功
 /**
  * 自动 获取所有小区地址信息
  * @param response
  * @return
  * @throws Exception
  */
  @ResponseBody
  @RequestMapping(value="/showAll", method=RequestMethod.GET, produces="text/html;charset=UTF-8")
  public String showAll(){
	 try {
		List<Location> list = this.locationService.showAll();
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("code", state_0);
		map.put("list", list);
		JSONObject jsonObject = JSONObject.fromObject(map);
		return jsonObject.toString();
	} catch (Exception e) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("code", state_500);
		JSONObject jsonObject = JSONObject.fromObject(map);
		return jsonObject.toString();
	}
  }
  /**
   * 获取指定小区对应的楼栋信息
   * @param record
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping(value="/showUnit", method=RequestMethod.GET, produces="text/html;charset=UTF-8")
  public String showUnit(Unitdoor record)throws Exception{
	try {
		String address = new String(record.getAddress().getBytes("ISO-8859-1"), "utf-8");
	    record.setAddress(address);
	    record.setNumber("all");
	    List<Unitdoor> list = this.unitdoorService.showUnitdoor(record);
	    Map<String, Object> map = new HashMap<String,Object>();
	    map.put("code",state_0);
	    map.put("list", list);
	    JSONObject jsonObject = JSONObject.fromObject(map);
	    return jsonObject.toString();
	} catch (Exception e) {
	    Map<String, Object> map = new HashMap<String,Object>();
	    map.put("code",state_500);
	    JSONObject jsonObject = JSONObject.fromObject(map);
	    return jsonObject.toString();
	}
  }
}
